﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using SMS.Contract;

namespace SMS.Console.Controls
{
    public partial class SystemManageControl : UserControl
    {
        public SystemManageControl()
        {
            InitializeComponent();
            try
            {
                var connState = ConnectionState;
                var serviceState = ServiceState;
                if (serviceState)
                {
                    connState = ConnectionState;
                    if (connState) SetText(txtInfo, "短信模块已启动。");
                }
                btnStart.Enabled = !connState;
                btnStop.Enabled = serviceState;
            }
            catch (Exception ex)
            {
                SetText(txtInfo, ex.Message);
            }
        }

        /// <summary>
        /// 获取服务是否已经启动
        /// </summary>
        private bool ServiceState
        {
            get
            {
                var serviceState = false;
                try
                {
                    serviceState = smsService.Status == ServiceControllerStatus.Running;
                }
                catch (Exception ex)
                {
                    SetText(txtInfo, ex.Message);
                }
                return serviceState;
            }
        }

        /// <summary>
        /// 获取连接状态
        /// </summary>
        private bool ConnectionState
        {
            get
            {
                if (!ServiceState) return false;
                var connectionState = false;
                try
                {
                    connectionState = SMSService.GetConnectionState();
                }
                catch (Exception ex)
                {
                    SetText(txtInfo, ex.Message);
                }
                return connectionState;
            }
        }

        private ISMSService m_SMSService;
        private ISMSService SMSService
        {
            get
            {
                if (m_SMSService == null) m_SMSService = new SMSClient();
                return m_SMSService;
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            new ConfigWizardForm().ShowDialog();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                //服务已经停止。
                if (smsService.Status == ServiceControllerStatus.Stopped)
                {
                    smsService.Start();
                    //等待服务服务状态为Running
                    WaitForStatus(ServiceControllerStatus.Running);
                }
                var message = string.Empty;
                var startOk = false;
                var serviceState = ServiceState;

                if (serviceState)
                {
                    startOk = SMSService.Start(out message);
                    SetText(txtInfo, message);
                }
                if (!serviceState)
                {
                    SetText(txtInfo, "启动短信服务失败。");
                }
                btnStart.Enabled = !startOk;
                btnStop.Enabled = serviceState;
            }
            catch (Exception ex)
            {
                SetText(txtInfo, ex.Message);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                //服务已经停止。
                if (smsService.Status == ServiceControllerStatus.Running)
                {
                    smsService.Stop();
                    //等待服务服务状态为Stopped
                    WaitForStatus(ServiceControllerStatus.Stopped);
                }
                var serviceState = ServiceState;
                btnStart.Enabled = !serviceState;
                btnStop.Enabled = serviceState;
                if (ServiceState)
                {
                    SetText(txtInfo, "短信服务停止失败。");
                }
                else
                {
                    SetText(txtInfo, "短信服务已经停止。");
                }
            }
            catch (Exception ex)
            {
                SetText(txtInfo, ex.Message);
            }
        }

        /// <summary>
        /// 等待服务达到指定状态。
        /// </summary>
        private void WaitForStatus(ServiceControllerStatus status)
        {
            smsService.WaitForStatus(status, new TimeSpan(0, 0, 30));
        }

        /// <summary>
        /// 在文本显示信息。
        /// </summary>
        private void SetText(RichTextBox textBox, string message)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new SetRichTextCallBack(SetText), new object[] { textBox, message });
            }
            else
            {
                textBox.AppendText(string.Format("{0}{1}", message, Environment.NewLine));
            }
        }
    }
}
